﻿using System;
using System.Collections;
using System.Reflection;
using EOS.Framework.Web.Mvc.Rendering;

namespace EOS.Framework.Web.Mvc.Controls
{
    public class RadioGroup : Control
    {
        private const string Space = " ";

        private IEnumerable enumerable;
        private string itemTextProperty;
        private string itemValueProperty;
        private object selected;
        private string itemClass;
        private bool firstChecked;
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public bool FirstChecked
        {
            get { return firstChecked; }
            set { firstChecked = value; }
        }

        public string ItemClass
        {
            get { return itemClass; }
            set { itemClass = value; }
        }

        public object Selected
        {
            get { return selected; }
            set { selected = value; }
        }

        public string ItemTextProperty
        {
            get { return itemTextProperty; }
            set { itemTextProperty = value; }
        }

        public string ItemValueProperty
        {
            get { return itemValueProperty; }
            set { itemValueProperty = value; }
        }

        public IEnumerable Enumerable
        {
            get { return enumerable; }
            set { enumerable = value; }
        }

        public RadioGroup(string name, IEnumerable enumerable)
            : this(name, enumerable, null)
        { }

        public RadioGroup(string name, IEnumerable enumerable, object selected)
            : this(name, enumerable, selected, null)
        { }

        public RadioGroup(string name, IEnumerable enumerable, object selected, string itemValueProperty)
            : this(name, enumerable, selected, itemValueProperty, null, null, false)
        { }

        public RadioGroup(string name, IEnumerable enumerable, object selected, string itemValueProperty, string itemTextProperty, string itemClass, bool firstChecked)
        {
            this.enumerable = enumerable;
            this.name = name;
            this.selected = selected;
            this.firstChecked = firstChecked;
            this.itemClass = itemClass;
            this.itemValueProperty = itemValueProperty;
            this.itemTextProperty = itemTextProperty;
        }

        protected override void OnRender(IXhtmlWriter writer, PageContext context)
        {
            WriteBeginAttachedTag(writer);

            int index = 0;
            foreach (object obj in enumerable)
            {
                string itemName = obj.ToString();
                if (!string.IsNullOrEmpty(itemTextProperty))
                {
                    PropertyInfo namePropertyInfo = obj.GetType().GetProperty(itemTextProperty);
                    if (namePropertyInfo != null)
                    {
                        itemName = namePropertyInfo.GetValue(obj, null).ToString();
                    }
                }

                string itemValue = obj.ToString();
                if (!string.IsNullOrEmpty(itemValueProperty))
                {
                    PropertyInfo valuePropertyInfo = obj.GetType().GetProperty(itemValueProperty);
                    if (valuePropertyInfo != null)
                    {
                        itemValue = valuePropertyInfo.GetValue(obj, null).ToString();
                    }
                }

                writer.WriteStartElement("div");
                if (string.IsNullOrEmpty(itemClass) == false)
                {
                    writer.WriteAttribute("class", itemClass);
                }

                writer.WriteStartElement("input");
                writer.WriteAttribute("type", "radio");
                writer.WriteAttribute("name", name);
                writer.WriteAttribute("value", itemValue);

                if ((selected != null && itemValue.Equals(selected)) || (index == 0 && firstChecked))
                {
                    writer.WriteAttribute("checked", "checked");
                }

                writer.WriteString(Space + itemName);
                writer.WriteEndElement();
                writer.WriteEndElement();

                index++;
            }

            WriteEndAttachedTag(writer);
        }
    }
}
